package com.sino.config.server.controller.project;

import org.nutz.dao.Cnd;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.sino.config.server.ConfigServerApplication.Consts.InstalledPermission;
import com.sino.config.server.bean.project.Profile;
import com.sino.config.server.controller.BaseController;
import com.sino.config.server.ext.anno.SINORequiresPermissions;
import com.sino.config.server.service.project.ProfileService;

import club.zhcs.common.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * @author kerbores
 *
 */
@RestController
@RequestMapping("profile")
@Api(value = "Profile", tags = { "Profile" })
public class ProfileController extends BaseController {

	@Autowired
	ProfileService profileService;

	/**
	 * 
	 * @param page
	 * @return
	 */
	@GetMapping("list")
	@SINORequiresPermissions(InstalledPermission.PROFILE_LIST)
	@ApiOperation("Profile列表")
	public Result list(@RequestParam(value = "page", defaultValue = "1") @ApiParam("页码") int page) {
		return Result.success().addData("pager", profileService.searchByPage(_fixPage(page), Cnd.NEW().asc("weight")));
	}

	/**
	 * 
	 * @param key
	 * @param page
	 * @return
	 */
	@GetMapping("search")
	@SINORequiresPermissions(InstalledPermission.PROFILE_LIST)
	@ApiOperation("Profile分页搜索")
	public Result search(@RequestParam("key") String key, @RequestParam(value = "page", defaultValue = "1") int page) {
		return Result.success().addData("pager", profileService.searchByKeyAndPage(_fixSearchKey(key), _fixPage(page), "name", "description").addParam("key", key));
	}

	/**
	 * 
	 * @param profile
	 * @return
	 */
	@PostMapping("add")
	@SINORequiresPermissions(InstalledPermission.PROFILE_ADD)
	@ApiOperation("Profile新增")
	public Result save(@RequestBody Profile profile) {
		return profileService.save(profile) == null ? Result.fail("保存Profile失败!") : Result.success().addData("profile", profile);
	}

	/**
	 * 
	 * @param id
	 * @return
	 */
	@GetMapping("{id}")
	@SINORequiresPermissions(value = { InstalledPermission.PROFILE_EDIT })
	@ApiOperation("Profile详情")
	public Result detail(@PathVariable("id") @ApiParam("Profileid") long id) {
		return Result.success().addData("profile", profileService.fetch(id));
	}

	/**
	 * 
	 * @param id
	 * @return
	 */
	@GetMapping("delete/{id}")
	@SINORequiresPermissions(value = { InstalledPermission.PROFILE_DELETE })
	@ApiOperation("删除Profile")
	public Result delete(@PathVariable("id") @ApiParam("Profileid") long id) {
		return profileService.delete(id) == 1 ? Result.success() : Result.fail("删除Profile失败!");
	}

	/**
	 * 
	 * @param profile
	 * @return
	 */
	@PostMapping("edit")
	@SINORequiresPermissions(InstalledPermission.PROFILE_EDIT)
	@ApiOperation("编辑Profile")
	public Result update(@RequestBody Profile profile) {
		return profileService.updateIgnoreNull(profile) != 1 ? Result.fail("更新profile失败!") : Result.success().addData("profile", profile);
	}
}
